﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Dynamic;

namespace DynamicTestApp.DynamicXMLReader.MCS_UK
{



    public class DynamicXmlParser : DynamicObject
    {

        XElement element;

        /// <summary>
        /// Initializes a new instance of the <see cref="DynamicXmlParser"/> class.
        /// </summary>
        /// <param name="filename">The filename.</param>
        public DynamicXmlParser(string filename)
        {
            element = XElement.Load(filename);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DynamicXmlParser"/> class.
        /// </summary>
        /// <param name="xml">The XML.</param>
        /// <param name="loadOptions">The load options.</param>
        public DynamicXmlParser(string xml, LoadOptions loadOptions)
        {
            element = XElement.Parse(xml,  loadOptions); 
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="DynamicXmlParser"/> class from being created.
        /// </summary>
        /// <param name="el">The el.</param>
        private DynamicXmlParser(XElement el)
        {
            element = el;
        }

        /// <summary>
        /// Provides the implementation for operations that get member values. Classes derived from the <see cref="T:System.Dynamic.DynamicObject"/> class can override this method to specify dynamic behavior for operations such as getting a value for a property.
        /// </summary>
        /// <param name="binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) statement, where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject"/> class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
        /// <param name="result">The result of the get operation. For example, if the method is called for a property, you can assign the property value to <paramref name="result"/>.</param>
        /// <returns>
        /// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.)
        /// </returns>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (element == null)
            {
                result = null;
                return false;
            }

            XElement sub = element.Element(binder.Name);

            if (sub == null)
            {
                result = null;
                return false;
            }
            else
            {
                result = new DynamicXmlParser(sub);
                return true;
            }
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            if (element != null)
            {
                return element.Value;
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets the <see cref="System.String"/> with the specified attr.
        /// </summary>
        public string this[string attr]
        {
            get
            {
                if (element == null)
                {
                    return string.Empty;
                }

                return element.Attribute(attr).Value;
            }
        }
    }
}
